package cn.xiehangs;

import com.badlogic.gdx.scenes.scene2d.Group;
import com.badlogic.gdx.scenes.scene2d.Touchable;

public class qi_zi_group extends Group {

    static cn.xiehangs.tou_zi tou_zi;

    int size;

    boolean is_robot = false,win=false;

    static int[] win_queue = new int[4];
    聊天框 聊天框_1;


    qi_zi number_1, number_2, number_3, number_4;

    public qi_zi_group(
            int size,
            qi_zi number_1,
            qi_zi number_2,
            qi_zi number_3,
            qi_zi number_4
    ) {
        this.size = size;
        this.number_1 = number_1;
        this.number_2 = number_2;
        this.number_3 = number_3;
        this.number_4 = number_4;
        begin();
    }

    public void set聊天框_1(聊天框 聊天框_1) {
        this.聊天框_1 = 聊天框_1;
        number_1.set聊天框(聊天框_1);
        number_2.set聊天框(聊天框_1);
        number_3.set聊天框(聊天框_1);
        number_4.set聊天框(聊天框_1);
    }

    public qi_zi_group(
            int size,
            qi_zi number_1,
            qi_zi number_2,
            qi_zi number_3,
            qi_zi number_4,
            boolean is_robot) {
        this.size = size;
        this.number_1 = number_1;
        this.number_2 = number_2;
        this.number_3 = number_3;
        this.number_4 = number_4;
        this.is_robot = is_robot;
        begin();
    }

    void begin() {
        this.addActor(number_1);
        this.addActor(number_2);
        this.addActor(number_3);
        this.addActor(number_4);
        win_queue[0] = -1;
        win_queue[1] = -1;
        win_queue[2] = -1;
        win_queue[3] = -1;
    }

    public static int add_win(int x){
        int i;
        for ( i=0;i<4;i++){
            if (win_queue[i]==-1){
                win_queue[i]=x;
                break;
            }
        }
        return i;
    }

    void is_win(){
        if(qi_zi.move_step_pool==size&&number_1.is_end&&number_2.is_end&&number_3.is_end&&number_4.is_end&&win==false){
            win=true;
            number_1.is_win=true;
            number_2.is_win=true;
            number_3.is_win=true;
            number_4.is_win=true;
            int sum=add_win(size);
            if (sum==3){
                if (聊天框_1!=null){
                    String first,second,third,fourth;
                    switch (win_queue[0]){
                        case 0:
                            first="第一名：红色";
                            break;
                        case 1:
                            first="第一名：蓝色";
                            break;
                        case 2:
                            first="第一名：黄色";
                            break;
                        case 3:
                            first="第一名：绿色";
                            break;
                        default:
                            first="第一名：无";
                            break;
                    }
                    switch (win_queue[1]){
                        case 0:
                            second="第二名：红色";
                            break;
                        case 1:
                            second="第二名：蓝色";
                            break;
                        case 2:
                            second="第二名：黄色";
                            break;
                        case 3:
                            second="第二名：绿色";
                            break;
                        default:
                            second="第二名：无";
                            break;
                    }
                    switch (win_queue[2]){
                        case 0:
                            third="第三名：红色";
                            break;
                        case 1:
                            third="第三名：蓝色";
                            break;
                        case 2:
                            third="第三名：黄色";
                            break;
                        case 3:
                            third="第三名：绿色";
                            break;
                        default:
                            third="第三名：无";
                            break;
                    }
                    switch (win_queue[3]){
                        case 0:
                            fourth="第四名：红色";
                            break;
                        case 1:
                            fourth="第四名：蓝色";
                            break;
                        case 2:
                            fourth="第四名：黄色";
                            break;
                        case 3:
                            fourth="第四名：绿色";
                            break;
                        default:
                            fourth="第四名：无";
                            break;
                    }
                    聊天框_1.chat(" ","游戏结束");
                    聊天框_1.chat(" ",first);
                    聊天框_1.chat(" ",second);
                    聊天框_1.chat(" ",third);
                    聊天框_1.chat(" ",fourth);
                }
            }
            int next = qi_zi.getMove_step_pool()+1;
            if(next==4){
                next=0;
            }
            qi_zi.setMove_step_pool(next);
        }
        else if(qi_zi.move_step_pool==size&&win==true){
            int next = qi_zi.getMove_step_pool()+1;
            if(next==4){
                next=0;
            }
            qi_zi.setMove_step_pool(next);
        }
    }

    public void robot() {
        if (is_robot && qi_zi.move_step_pool == size&& qi_zi.is_move_pool==true&&tou_zi.is_dong_hua==false&&this.win==false) {
            qi_zi.is_move_pool=false;
            tou_zi.act_dong_hua(tou_zi.rd.nextInt(6) + 1);
            qi_zi.is_move_pool=false;
            int step = tou_zi.get_dian_shu();
            if (step == 6) {
                if (number_1.step == -1 && !number_1.is_end) {
                    qi_zi.setMove_step_pool(number_1.move_action(step));
                    return;
                } else if (number_2.step == -1 && !number_2.is_end) {
                    qi_zi.setMove_step_pool(number_2.move_action(step));
                    return;
                } else if (number_3.step == -1 && !number_3.is_end) {
                    qi_zi.setMove_step_pool(number_3.move_action(step));
                    return;
                } else if (number_4.step == -1 && !number_4.is_end) {
                    qi_zi.setMove_step_pool(number_4.move_action(step));
                    return;
                }
            }
                //从剩下的已经出发的棋子中随机选择一个
                int temp = tou_zi.rd.nextInt(4);
                if (temp == 0) {
                    if (number_1.step != -1&&!number_1.is_end) {
                        qi_zi.setMove_step_pool(number_1.move_action(step));
                    } else if (number_2.step != -1&&!number_2.is_end) {
                        qi_zi.setMove_step_pool(number_2.move_action(step));
                    } else if (number_3.step != -1&&!number_3.is_end) {
                        qi_zi.setMove_step_pool(number_3.move_action(step));
                    } else if (number_4.step != -1&&!number_4.is_end) {
                        qi_zi.setMove_step_pool(number_4.move_action(step));
                    }
                    else {
                        if (!number_1.is_end)
                         qi_zi.setMove_step_pool(number_1.move_action(step));
                        else if (!number_2.is_end)
                            qi_zi.setMove_step_pool(number_2.move_action(step));
                        else if (!number_3.is_end)
                            qi_zi.setMove_step_pool(number_3.move_action(step));
                        else if (!number_4.is_end)
                            qi_zi.setMove_step_pool(number_4.move_action(step));
                    }
                } else if (temp == 1) {
                    if (number_2.step != -1&&!number_2.is_end) {
                        qi_zi.setMove_step_pool(number_2.move_action(step));
                    } else if (number_3.step != -1&&!number_3.is_end) {
                        qi_zi.setMove_step_pool(number_3.move_action(step));
                    } else if (number_4.step != -1&&!number_4.is_end) {
                        qi_zi.setMove_step_pool(number_4.move_action(step));
                    } else if (number_1.step != -1&&!number_1.is_end) {
                        qi_zi.setMove_step_pool(number_1.move_action(step));
                    }
                    else {
                        if (!number_1.is_end)
                            qi_zi.setMove_step_pool(number_1.move_action(step));
                        else if (!number_2.is_end)
                            qi_zi.setMove_step_pool(number_2.move_action(step));
                        else if (!number_3.is_end)
                            qi_zi.setMove_step_pool(number_3.move_action(step));
                        else if (!number_4.is_end)
                            qi_zi.setMove_step_pool(number_4.move_action(step));
                    }
                } else if (temp == 2) {
                    if (number_3.step != -1&&!number_3.is_end) {
                        qi_zi.setMove_step_pool(number_3.move_action(step));
                    } else if (number_4.step != -1&&!number_4.is_end) {
                        qi_zi.setMove_step_pool(number_4.move_action(step));
                    } else if (number_1.step != -1&&!number_1.is_end) {
                        qi_zi.setMove_step_pool(number_1.move_action(step));
                    } else if (number_2.step != -1&&!number_2.is_end) {
                        qi_zi.setMove_step_pool(number_2.move_action(step));
                    }
                    else {
                        if (!number_1.is_end)
                            qi_zi.setMove_step_pool(number_1.move_action(step));
                        else if (!number_2.is_end)
                            qi_zi.setMove_step_pool(number_2.move_action(step));
                        else if (!number_3.is_end)
                            qi_zi.setMove_step_pool(number_3.move_action(step));
                        else if (!number_4.is_end)
                            qi_zi.setMove_step_pool(number_4.move_action(step));
                    }
                } else if (temp == 3) {
                    if (number_4.step != -1 && !number_4.is_end) {
                        qi_zi.setMove_step_pool(number_4.move_action(step));
                    } else if
                    (number_1.step != -1 && !number_1.is_end) {
                        qi_zi.setMove_step_pool(number_1.move_action(step));
                    } else if (number_2.step != -1 && !number_2.is_end) {
                        qi_zi.setMove_step_pool(number_2.move_action(step));
                    } else if (number_3.step != -1 && !number_3.is_end) {
                        qi_zi.setMove_step_pool(number_3.move_action(step));
                    } else {
                        if (!number_1.is_end)
                            qi_zi.setMove_step_pool(number_1.move_action(step));
                        else if (!number_2.is_end)
                            qi_zi.setMove_step_pool(number_2.move_action(step));
                        else if (!number_3.is_end)
                            qi_zi.setMove_step_pool(number_3.move_action(step));
                        else if (!number_4.is_end)
                            qi_zi.setMove_step_pool(number_4.move_action(step));
                    }
                }

        }
    }
    public void chu_shi_hua(){
        number_1.chu_shi_hua();
        number_2.chu_shi_hua();
        number_3.chu_shi_hua();
        number_4.chu_shi_hua();
        win=false;
        win_queue[0] = -1;
        win_queue[1] = -1;
        win_queue[2] = -1;
        win_queue[3] = -1;
    }
}
